﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using BusinessObjects;

namespace DataAccess.SqlServer
{
    public class SqlServerHinhThucHopDong: IHinhThucHopDong
    {

        public HinhThucHopDong GetHinhThucHopDong(int oid)
        {
            string sql = @" Select MaQuanLy, TenHinhThucHopDong, CoThoiHan, SoThang
                            From HinhThucHopDong
                            Where Oid = @Oid";
            SqlParameter param = new SqlParameter("@Oid", oid);
            DataRow row = DataProvider.GetDataRow(sql, param);

            HinhThucHopDong hinhThucHopDong = new HinhThucHopDong();

            hinhThucHopDong.Oid = oid;
            hinhThucHopDong.MaQuanLy = row["MaQuanLy"].ToString();
            hinhThucHopDong.TenHinhThucHopDong = row["TenHinhThucHopDong"].ToString();
            hinhThucHopDong.CoThoiHan = bool.Parse(row["CoThoiHan"].ToString());
            hinhThucHopDong.SoThang = byte.Parse(row["SoThang"].ToString());

            return hinhThucHopDong;
        }

        public List<HinhThucHopDong> GetListHinhThucHopDong()
        {
            string sql = @" Select Oid, MaQuanLy, TenHinhThucHopDong, CoThoiHan, SoThang
                            From HinhThucHopDong";

            DataTable dt = DataProvider.GetDataTable(sql);
            List<HinhThucHopDong> list = new List<HinhThucHopDong>();

            foreach (DataRow row in dt.Rows)
            {
                HinhThucHopDong hinhThucHopDong = new HinhThucHopDong();

                hinhThucHopDong.Oid = int.Parse(row["Oid"].ToString());
                hinhThucHopDong.MaQuanLy = row["MaQuanLy"].ToString();
                hinhThucHopDong.TenHinhThucHopDong = row["TenHinhThucHopDong"].ToString();
                hinhThucHopDong.CoThoiHan = bool.Parse(row["CoThoiHan"].ToString());
                hinhThucHopDong.SoThang = byte.Parse(row["SoThang"].ToString());
                list.Add(hinhThucHopDong);
            }
            return list;
        }

        public int Insert(HinhThucHopDong hinhThucHopDong)
        {
            string sql = @" Insert into HinhThucHopDong(MaQuanLy, TenHinhThucHopDong, CoThoiHan, SoThang)
                            Values(@MaQuanLy, @TenHinhThucHopDong, @CoThoiHan, @SoThang)";

            SqlParameter[] param = {
                                       new SqlParameter("@MaQuanLy", hinhThucHopDong.MaQuanLy),
                                       new SqlParameter("@TenHinhThucHopDong", hinhThucHopDong.TenHinhThucHopDong),
                                       new SqlParameter("@CoThoiHan", hinhThucHopDong.CoThoiHan),
                                       new SqlParameter("@SoThang", hinhThucHopDong.SoThang)
                                   };
            int id= DataProvider.Insert(sql,true, param);
            if (id > 0)
                hinhThucHopDong.Oid = id;
            return hinhThucHopDong.Oid;         
        }

        public int Update(HinhThucHopDong hinhThucHopDong)
        {
            string sql = @" Update HinhThucHopDong
                            Set MaQuanLy = @MaQuanLy,
                                TenHinhThucHopDong = @TenHinhThucHopDong,
                                CoThoiHan = @CoThoiHan,
                                SoThang = @SoThang
                            Where Oid = @Oid";

            SqlParameter[] param = {
                                       new SqlParameter("@Oid", hinhThucHopDong.Oid),
                                       new SqlParameter("@MaQuanLy", hinhThucHopDong.MaQuanLy),
                                       new SqlParameter("@TenHinhThucHopDong", hinhThucHopDong.TenHinhThucHopDong),
                                       new SqlParameter("@CoThoiHan", hinhThucHopDong.CoThoiHan),
                                       new SqlParameter("@SoThang", hinhThucHopDong.SoThang)
                                   };
            return DataProvider.Update(sql, param);
        }

        public int Delete(HinhThucHopDong hinhThucHopDong)
        {
            string sql = @" Delete from HinhThucHopDong where Oid = @Oid";

            SqlParameter param = new SqlParameter("@Oid", hinhThucHopDong.Oid);

            return DataProvider.Update(sql, param);
        }


        public int Save(HinhThucHopDong obj)
        {
            throw new NotImplementedException();
        }
    }
}
